package main

import (
	"fmt"
	"golang.org/x/text/encoding/simplifiedchinese"
	"io/ioutil"
	"net/http"
)

type Charset string

func main() {
	c := make(chan string)
	go getString("https://gank.io/api/today", c)
	s := <-c
	fmt.Println(s)
}

func getString(url string, c chan string) {
	response, err := http.Get(url)
	if err != nil {
		_ = fmt.Errorf("错误%v", err)
		return
	}

	bytes, _ := ioutil.ReadAll(response.Body)
	s := string(bytes)
	c <- s
}

func postString(url string, c chan string) {
	response, err := http.Get(url)
	if err != nil {
		_ = fmt.Errorf("错误%v", err)
		return
	}

	bytes, _ := ioutil.ReadAll(response.Body)
	s := string(bytes)
	c <- s
}

const (
	UTF8    = Charset("UTF-8")
	GB18030 = Charset("GB18030")
)

func ConvertByte2String(byte []byte, charset Charset) string {

	var str string
	switch charset {
	case GB18030:
		var decodeBytes, _ = simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
		str = string(decodeBytes)
	case UTF8:
		fallthrough
	default:
		str = string(byte)
	}

	return str
}
